package com.bro.person.admin.utils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author swallow
 * Created by swallow on 2018/11/13.<br>
 */
public class Calcaulate {

    /**
     *提前将 符号的优先级定义好
     */
    private static final Map<Character, Integer> basic = new HashMap<Character, Integer>();
    static {
        basic.put('-', 1);
        basic.put('+', 1);
        basic.put('*', 2);
        basic.put('/', 2);
        //在运算中  （）的优先级最高，但是此处因程序中需要 故设置为0
        basic.put('(', 0);
    }

    public static void main(String[] args) {
        String infixExpression = "92+(88*(2-5))+100/22";
        //1. 转换成 后缀表达式
        List<String> queue = new ArrayList<>();
        char[] charArr = infixExpression.trim().toCharArray();
        Stack<Character> stack = new Stack<Character>();
        String standard = "*/+-()";
        int len = 0;
        for (int i = 0; i < charArr.length; i++) {
            char ch = infixExpression.charAt(i);
            if(Character.isDigit(ch)){
                len ++;
            }else if(ch == '.'){
                len ++;
            }else if(standard.indexOf(ch)!=-1){
                if(len>0){
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr,i-len,i)));
                    len = 0;
                }
                if(ch == '('){
                    stack.add(ch);
                    continue;
                }
                if(!stack.isEmpty()){
                    int size = stack.size() - 1;
                    boolean flag = false;
                    while(size>=0 && ch==')' && stack.get(size)!='('){
                        queue.add(String.valueOf(stack.remove(size)));
                        size --;
                        flag = true;
                    }
                    while(size>=0 && !flag && basic.get(stack.get(size))>=basic.get(ch)){
                        queue.add(String.valueOf(stack.remove(size)));
                        size --;
                    }
                }
                if(ch!=')'){
                    stack.add(ch);
                }else{
                    stack.remove(stack.size() -1);

                }
            }
            if(i==charArr.length-1){
                if(len>0){
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr,i-len+1,i+1)));
                    len = 0;
                }
                int size = stack.size() -1;
                while(size>=0){
                    queue.add(String.valueOf(stack.remove(size)));
                    size --;
                }
            }
        }
        String arrString = queue.stream().collect(Collectors.joining(","));
        List<String> list = new ArrayList<>();
        //2. 计算
        String[] arr = arrString.split(",");
        for (int i = 0; i < arr.length; i++) {
            switch (arr[i]) {
                case "+": double a = Double.parseDouble(list.remove(list.size() - 2))+ Double.parseDouble(list.remove(list.size() - 1)); list.add(String.valueOf(a));     break;
                case "-": double b = Double.parseDouble(list.remove(list.size() - 2))- Double.parseDouble(list.remove(list.size() - 1)); list.add(String.valueOf(b));     break;
                case "*": double c = Double.parseDouble(list.remove(list.size() - 2))* Double.parseDouble(list.remove(list.size() - 1)); list.add(String.valueOf(c));     break;
                case "/": double d = Double.parseDouble(list.remove(list.size() - 2))/ Double.parseDouble(list.remove(list.size() - 1)); list.add(String.valueOf(d));       break;
                default: list.add(arr[i]);     break;
            }
        }
        System.out.println(list.toString());
    }
}
